package one;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Myjavaben.Employee;
import utilmyio.MyIOper;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JLabel;

public class MyJFrame extends JFrame {

	private JPanel contentPane;
	ArrayList<Employee> relist=new ArrayList<Employee>();
	ArrayList<Employee> anlist=new ArrayList<Employee>();
	private JTextField textField;
	private String result=null;
	MyIOper myi=new MyIOper();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyJFrame frame = new MyJFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 541, 386);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmOpen = new JMenuItem("Open");
		mntmOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//open File dial
				JFileChooser jf=new JFileChooser();
				if(jf.showOpenDialog(MyJFrame.this)==JFileChooser.APPROVE_OPTION) {
					String filename=jf.getSelectedFile().getAbsolutePath();
					//System.out.println(filename);
					//read data
					//MyIOper myi=new MyIOper();
					relist=myi.getFileData(filename);

				}
				
				
				
			}
		});
		mnFile.add(mntmOpen);
		
		JMenuItem mntmSave = new JMenuItem("Save");
		mntmSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jf=new JFileChooser();
				if(jf.showSaveDialog(MyJFrame.this)==JFileChooser.APPROVE_OPTION) {
					String filename=jf.getSelectedFile().getAbsolutePath();
					//System.out.println(filename);
					//read data
					//MyIOper myi=new MyIOper();
					myi.setFileData(filename, anlist);
					
				}
			}
		});
		mnFile.add(mntmSave);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(98, 10, 242, 27);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnSearch = new JButton("search by dept");

		btnSearch.setBounds(350, 12, 123, 23);
		contentPane.add(btnSearch);
		
		JTextArea textArea = new JTextArea();
		textArea.setBounds(34, 59, 439, 244);
		contentPane.add(textArea);
		
		JLabel lblInput = new JLabel("Input :");
		lblInput.setBounds(34, 16, 54, 21);
		contentPane.add(lblInput);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String input=textField.getText().trim();
				if(input.length()==0) {
					anlist.clear();
					textArea.setText("");
					for(Employee ee:relist) {
						textArea.append(ee.getName()+" "+ee.getDept()+" "+ee.getPhone()+"\n");
						anlist.add(ee);
						
					}
					
				result=textArea.getText().trim();
				anlist=relist;
				}else {
					anlist.clear();
					textArea.setText("");
					for(Employee e1:relist) {
						if(e1.getDept().indexOf(input)!=-1) {
							textArea.append(e1.toString()+"\n");
							anlist.add(e1);
						}//end of if
					}//end of for
					result=textArea.getText().trim();
				}
			}
		});
	}
}
